package com.ideaaedi.springcloud.jd.commonspring.config;

import com.ideaaedi.commonspring.decorator.MDCTaskDecorator;
import com.ideaaedi.commonspring.threadpool.ThreadPoolBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 通用线程池
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 2021.0.1.A
 */
@Configuration
public class ThreadPoolConfig {
    
    public static final String COMMON_EXECUTOR_NAME = "commonExecutor";
    
    @Bean(COMMON_EXECUTOR_NAME)
    public ThreadPoolTaskExecutor acsCommonExecutor() {
        int processors = Runtime.getRuntime().availableProcessors();
        return ThreadPoolBuilder.builder()
                .corePoolSize(2 * processors + 1)
                .maxPoolSize(100)
                .queueCapacity(500)
                .keepAliveSeconds(60)
                .threadNamePrefix("common-pool-")
                .rejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy())
                .taskDecorator(new MDCTaskDecorator())
                .buildAndInitialize();
    }
}
